package com.xiyan.mocklocation.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiyan.mocklocation.domain.CorePerson;
import com.xiyan.mocklocation.domain.DeviceCard;
import com.xiyan.mocklocation.domain.PersonVO;
import com.xiyan.mocklocation.domain.PositionCurrent;
import com.xiyan.mocklocation.mapper.CorePersonMapper;
import com.xiyan.mocklocation.mapper.DeviceCardMapper;
import com.xiyan.mocklocation.mapper.PositionCurrentMapper;
import com.xiyan.mocklocation.utils.RandomDataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class GenerateLocationTask {
    @Autowired
    private PositionCurrentMapper positionCurrentMapper;

    @Autowired
    private CorePersonMapper corePersonMapper;

    @Autowired
    private DeviceCardMapper deviceCardMapper;

    @PostConstruct
    public void initRun() {
        cleanAndInitCard(); //清理和新增定位卡
        cleanAndInitPositionAndPerson(); //清理和新增实时定位和人员
    }


    private void cleanAndInitPositionAndPerson() {
        LocalDateTime now = LocalDateTime.now();
        int r = positionCurrentMapper.delete(new LambdaQueryWrapper<PositionCurrent>().gt(PositionCurrent::getPersonId, RandomDataUtils.personBasicId));
        int p = corePersonMapper.delete(new LambdaQueryWrapper<CorePerson>().gt(CorePerson::getPersonId, RandomDataUtils.personBasicId));
        log.info("删除实时定位结果：{}，删除人员结果：{}", r,p);
        List<PositionCurrent> currentList=new ArrayList<>();
        List<CorePerson> personList=new ArrayList<>();
        List<PersonVO> list = RandomDataUtils.getPersonVO();
        list.forEach(t -> {
            PositionCurrent positionCurrent = PositionCurrent.builder()
                    .acceptTime(now)
                    .personId(t.getPersonId())
                    .realName(t.getPersonName())
                    .cardId(t.getCardId())
                    .layerId(t.getLayerId())
                    .longitude(t.getLongitude())
                    .latitude(t.getLatitude())
                    .beaconId(-1)
                    .personType("staff")
                    .stillStatus(0)
                    .positionType("1")
                    .build();
            currentList.add(positionCurrent);
            CorePerson corePerson=CorePerson.builder()
                    .personId(t.getPersonId())
                    .realName(t.getPersonName())
                    .cardId(t.getCardId())
                    .createTime(now)
                    .build();
            personList.add(corePerson);
        });
        int i=positionCurrentMapper.insertBatchSomeColumn(currentList);
        LocalDateTime endTime=LocalDateTime.now();
        long ms = Duration.between(now, endTime).toMillis();
        log.info("新增实时定位完成：{}，结果：{}，耗时：{}毫秒", endTime,i,ms);
        int c=corePersonMapper.insertBatchSomeColumn(personList);
        long cms = Duration.between(now, LocalDateTime.now()).toMillis();
        log.info("新增人员完成：{}，结果：{}，耗时：{}毫秒", endTime,c,cms);
    }
    public void cleanAndInitCard() {
        LocalDateTime now = LocalDateTime.now();
        int r = deviceCardMapper.delete(new LambdaQueryWrapper<DeviceCard>().gt(DeviceCard::getCardId, RandomDataUtils.cardBasicId));
        log.info("删除卡结果：{}", r);
        List<DeviceCard> cardList=new ArrayList<>();
        List<Long> list = RandomDataUtils.getCardId();
        list.forEach(cardId -> {
            DeviceCard deviceCard = DeviceCard.builder()
                    .cardId(cardId)
                    .cardType("card")
                    .cardEnable("Y")
                    .useStatus("Y")
                    .cardStatus(-1)
                    .createTime(now)
                    .build();
            cardList.add(deviceCard);
        });
        int i=deviceCardMapper.insertBatchSomeColumn(cardList);
        LocalDateTime endTime=LocalDateTime.now();
        long ms = Duration.between(now, endTime).toMillis();
        log.info("新增定位卡完成：{}，结果：{}，耗时：{}毫秒", endTime,i,ms);
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    public void update() {
        LocalDateTime now = LocalDateTime.now();
        log.info("准备更新定位：{}", now);
        List<PersonVO> list = RandomDataUtils.getPersonVO();
        list.forEach(t -> {
            PositionCurrent positionCurrent = PositionCurrent.builder()
                    .acceptTime(now)
                    .personId(t.getPersonId())
                    .realName(t.getPersonName())
                    .cardId(t.getCardId())
                    .layerId(t.getLayerId())
                    .longitude(t.getLongitude())
                    .latitude(t.getLatitude())
                    .beaconId(-1)
                    .personType("staff")
                    .stillStatus(0)
                    .positionType("1")
                    .build();
            positionCurrentMapper.update(positionCurrent,new LambdaQueryWrapper<PositionCurrent>().eq(PositionCurrent::getPersonId,t.getPersonId()));
        });
        LocalDateTime endTime=LocalDateTime.now();
        long ms = Duration.between(now, endTime).toMillis();
        log.info("更新定位完成：{}，耗时：{}毫秒", endTime,ms);
    }

}
